package com.wanda.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.PurposeModel;
import com.wanda.service.PurposeService;
import com.wanda.util.DateFormat;


@Controller
@RequestMapping("/ht/purpose")
public class PurposeController {

	@Autowired
	private PurposeService purposeService;
	
	//查询所有支付信息
	@RequestMapping(value="/selectAll.do")
	public ModelAndView selectAll(){
		
		System.out.println("进入用途的查询所有方法");
		
		ModelAndView model = new ModelAndView();
		
		List<PurposeModel> pay = null;
		try{
			pay=purposeService.selectAll();
			model.setViewName("/ht/listPurpose.jsp");
			model.addObject("purposes", pay);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return model;	
	}
	//房间需要用途 的名字
		@RequestMapping(value="/selectAlls.do")
		public @ResponseBody Map<String,Object> showAlls(String id){
			
			Map<String,Object> map = new HashMap<String,Object>();

			List<PurposeModel> model = null;
			model=purposeService.selectAll();
			map.put("msg",model);
			
			return map;	
		}
		
		//添加付款方式
		@RequestMapping(value="/addPurpose.do")
		public @ResponseBody Map<String,Object> add(String purpose_id,String purpose){
			
			System.out.println("进入添加用途");
			PurposeModel models = new PurposeModel();
			
			models.setPurpose_id(purpose_id);
			models.setPurpose(purpose);

			purposeService.insertPurpose(models);
			
			Map<String,Object> map = new HashMap<String,Object>();
						
			map.put("msg", models);
			return map;		
		}
		
		//修改用途方式
		@RequestMapping(value="/updatePurpose.do")
		public String update(PurposeModel model){
			
			System.out.println("进入修改用途");
			
			model.setUpdate_time(DateFormat.getNowDate());
			
			purposeService.updatePurpose(model);
			
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("msg", model);
			return "redirect:/ht/purpose/selectAll.do";
		
		}
		
		//删除用途方式
		@RequestMapping(value="/deletePurpose.do")
		public String delete(PurposeModel model){
			
			System.out.println("进入用途删除");
			model.setPurpose_id(model.getPurpose_id());
			model.setDelete_time(DateFormat.getNowDate());
			
			int id = purposeService.deletePurpose(model);
			
            Map<String,Object> map = new HashMap<String,Object>();
			
			map.put("msg", id);
			return "redirect:/ht/purpose/selectAll.do";
		}
	
		//查询某条用途
		@RequestMapping(value="/selectById.do")
		public ModelAndView selectById(String id){
			
			System.out.println("查询某条ID");
			ModelAndView and =new ModelAndView();
			
			PurposeModel model = null;
			try{
				model=purposeService.selectById(id);
				and.setViewName("/ht/updatePurpose.jsp");
				and.addObject("purs", model);
				
			}catch(Exception ex){
				ex.printStackTrace();
			}
			
			return and;
		}
}
